package com.afkt.project.ui.slice;



import com.afkt.project.base.app.BaseActivity;
import com.afkt.project.ui.adapter.TextAdapter;
import com.dev.utils.app.ShapeUtils;
import com.dev.widget.ui.WrapView;
import com.ohos.afkt.ResourceTable;
import com.dev.base.widget.BaseTextView;
import com.dev.utils.app.ResourceUtils;
import com.dev.utils.app.SizeUtils;
import com.dev.utils.app.helper.QuickHelper;
import com.dev.utils.app.helper.ViewHelper;
import com.dev.utils.common.ChineseUtils;
import com.dev.utils.common.RandomUtils;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;

import java.util.ArrayList;
import java.util.List;

public class FlexboxLayoutManagerSlice extends BaseActivity {

    private DirectionalLayout bar_Layout;
    private DirectionalLayout bar;
    private Image select_retuen;
    private WrapView vidAwWrapview;
    @Override
    protected int baseLayoutId() {
        return ResourceTable.Layout_base_view_recyclerview_flex;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void onBackground() {
        super.onBackground();
        bar_Layout.removeAllComponents();
    }

    @Override
    protected void initListener() {
        select_retuen.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                // LogUtils.error(TAG, "return 点击");
                onBackPressed();
            }
        });
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);

        bar_Layout = (DirectionalLayout) findComponentById(ResourceTable.Id_base_view_barLayout);
        bar = (DirectionalLayout) LayoutScatter.getInstance(this).parse(ResourceTable.Layout_sheet_dialog_bar, null, false);
        Text text = (Text) bar.findComponentById(ResourceTable.Id_titleText);
        select_retuen = (Image) bar.findComponentById(ResourceTable.Id_sheet_dialog_return);
        text.setText("FlexboxLayoutManager");
        text.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);

        bar_Layout.addComponent(bar);
        vidAwWrapview =(WrapView) findComponentById(ResourceTable.Id_vid_aw_wrapview);
        Component parent = findComponentById(ResourceTable.Id_vid_bvr_base);
        if (parent instanceof ComponentContainer) {
            ViewHelper.get()
                    .setPadding(parent, 0, 0, SizeUtils.dipConvertPx(5F)
                    , SizeUtils.dipConvertPx(5F));
            Component view = QuickHelper.get(new BaseTextView(this))
                    .setText("刷新")
                    .setBold()
                    .setTextColor(ResourceUtils.getColor(ResourceTable.Color_red))
                    .setTextSizeBySp(15.0f)
                    .setPaddingLeft(600)
                    .setPaddingTop(60)
                    .setTextSize(50)
                    .setOnClicks(com -> { initValue();}).getView();
            if (bar instanceof ComponentContainer) {
                bar.addComponent(view);
            }
        }
        initValue();
    }

    public void initValue() {

        vidAwWrapview.removeAllComponents();
        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_CONTENT,
                DirectionalLayout.LayoutConfig.MATCH_CONTENT
        );
        ShapeElement drawable = ShapeUtils.newShape(30f, ResourceUtils.getColor(ResourceTable.Color_color_11)).getmDrawable();
        for (int i = 1; i <= 20; i++) {
            String text = ChineseUtils.randomWord(RandomUtils.getRandom(8)) +
                    RandomUtils.getRandomLetters(RandomUtils.getRandom(8));
            String randomText = (new Integer(i).toString()) + "." + RandomUtils.getRandom(text.toCharArray(), text.length());
            BaseTextView baseTextView = createView(randomText, layoutConfig, drawable);

            vidAwWrapview.addComponent(baseTextView);
        }
        vidAwWrapview.refreshDraw();

    }

    private BaseTextView createView(String text, ComponentContainer.LayoutConfig layoutConfig,
                                    ShapeElement drawable) {

        BaseTextView text1 = (BaseTextView) QuickHelper.get(new BaseTextView(this))
                .setLayoutParams(layoutConfig)
                .setPadding(30, 15, 30, 15)
                .setBackground(drawable)
                .setMaxLines(1)
                .setTextColor(Color.WHITE.getValue())
                .setTextSizeBySp(15f)
                .setBold(RandomUtils.nextBoolean())
                .setText(text)
                .getView();
        text1.setTruncationMode(Text.TruncationMode.ELLIPSIS_AT_END);
        return text1;
    }
}
